package com.tjl.warehouse.controller;

import com.tjl.warehouse.annotation.AuthToken;
import com.tjl.warehouse.common.R;
import com.tjl.warehouse.exception.ThrowUtils;
import com.tjl.warehouse.model.MyPage;
import com.tjl.warehouse.model.Role;
import com.tjl.warehouse.model.dto.RoleDto;
import com.tjl.warehouse.service.RoleService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Tang
 * @createDate 2024/5/29
 */
@RestController
@Slf4j
@Tag(name = "角色接口")
@AuthToken
@RequestMapping("/role")
public class RoleController {

    @Resource
    RoleService roleService;

    @GetMapping("/role-list")
    public R<List<Role>> getRoleList() {

        List<Role> roleList = roleService.getRoleList();

        return R.success(roleList);
    }

    @GetMapping("/role-page-list")
    public R<MyPage<Role>> getRolePageList(RoleDto roleDto,MyPage<Role> page) {
        //检查参数
        ThrowUtils.throwIfParams(page == null);
        ThrowUtils.throwIfParams(page.getPageNum() <= 0,"页码最小为1");
        ThrowUtils.throwIfParams(page.getPageSize() >= 500,"最大每页500条");

        MyPage<Role> rolePage = roleService.getRolePage(roleDto, page);

        return R.success(rolePage);
    }

    @PutMapping("/role-update")
    public R<Boolean> updateRole(@RequestBody RoleDto roleDto) {

        ThrowUtils.throwIfParams(roleDto == null);
        ThrowUtils.throwIfParams(roleDto.getRoleId() <= 0,"id为空");

        Role role = new Role();
        BeanUtils.copyProperties(roleDto, role);
        log.info("修改的角色信息：{}" ,role);
        boolean update = roleService.updateRole(role);

        return R.success(update);
    }

    //修改启用状态
    @PutMapping("/role-state-update")
    public R<Boolean> updateRoleState(@RequestBody RoleDto roleDto) {
        ThrowUtils.throwIfParams(roleDto == null);
        ThrowUtils.throwIfParams(roleDto.getRoleId() <= 0,"id为空");
        Role role = new Role();
        BeanUtils.copyProperties(roleDto, role);
        boolean b = roleService.updateById(role);
        return b ? R.success(true) : R.error(false);
    }



}
